package com.zhaojp.demo.helper;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Service
public class RedisService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;


    @Resource(name = "stringRedisTemplate")
    private ValueOperations<String, String> valOpsStr;

    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;

    @Resource(name = "redisTemplate")
    private ValueOperations<Object, Object> valOpsObj;

    /**
     * 根据指定key获取String
     * @param key
     * @return
     */
    public String getStr(String key){
        return valOpsStr.get(key);
    }

    /**
     * 设置Str缓存
     * @param key
     * @param val
     */
    public void setStr(String key, String val){
        valOpsStr.set(key,val);
    }

    /**
     * 设置Str缓存 带失效时间
     * @param key
     * @param val
     * @param time
     * @param unit
     */
    public void setStr(String key, String val, long time, TimeUnit unit){
        valOpsStr.set(key,val,time,unit);
    }

    /**
     * 删除指定key
     * @param key
     */
    public void del(String key){
        stringRedisTemplate.delete(key);
    }

    /**
     * 根据指定o获取Object
     * @param o
     * @return
     */
    public Object getObj(Object o){
        return valOpsObj.get(o);
    }

    /**
     * 设置obj缓存
     * @param o1
     * @param o2
     */
    public void setObj(Object o1, Object o2){
        valOpsObj.set(o1, o2);
    }

    /**
     * 删除Obj缓存
     * @param o
     */
    public void delObj(Object o){
        redisTemplate.delete(o);
    }

    /**
     * 设置obj缓存 带失效时间
     * @param o1
     * @param o2
     * @param time
     * @param unit
     */
    public void setObj(Object o1, Object o2, long time, TimeUnit unit){
        valOpsObj.set(o1,o2,time,unit);
    }

    /**
     * 自增减
     * @param num
     * @return
     */
    public Long increment(String key, int num){
        return stringRedisTemplate.boundValueOps(key).increment(num);
    }

    /**
     * 根据key获取过期时间并换算成指定单位
     * @param key
     * @param u
     * @return
     */
    public Long getExpire(Object key,TimeUnit u){
        return redisTemplate.getExpire(key,u);
    }

    /**
     * 设置过期事件
     * @param key
     * @param time
     * @param unit
     * @return
     */
    public boolean expire(Object key, long time, TimeUnit unit){
        return redisTemplate.expire(key, time, unit);
    }

    /**
     * 删除,根据键的集合，批量删除值
     * @param keys
     */
    public void delete(Collection<Object> keys) {
        redisTemplate.delete(keys);
    }

    /**
     * 是否含有指定的键
     * @param key
     * @return
     */
    public Boolean hasKey(Object key) {
        return redisTemplate.hasKey(key);
    }

    /**
     * 向指定key中存放set集合
     * @param key
     * @param arr
     * @return
     */
    public Long addSet(Object key, Set arr){
        return redisTemplate.opsForSet().add(key,arr);
    }

    /**
     * 检查是否是set集合中的元素
     * @param key
     * @param mem
     * @return
     */
    public boolean isMember(Object key, Object mem) {
        return redisTemplate.opsForSet().isMember(key, mem);
    }

    /**
     * 返回set集合数据
     * @param key
     * @return
     */
    public Set members(Object key){
        return redisTemplate.opsForSet().members(key);
    }
}
